package cn.jacksigxu.min3halla.block.drink;

import cn.jacksigxu.min3halla.init.MHBlockStateProperties;
import cn.jacksigxu.min3halla.item.DrinkItem;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.VoxelShape;

import javax.annotation.Nullable;

public class MutableAlcoholDrinkBlock extends SizedDrinkBlock {

    public static final IntegerProperty ALCOHOL = MHBlockStateProperties.ALCOHOL;

    public MutableAlcoholDrinkBlock(VoxelShape shape) {
        this(shape, shape);
    }

    public MutableAlcoholDrinkBlock(VoxelShape shape, VoxelShape bigShape) {
        this(shape, shape, bigShape, bigShape);
    }

    public MutableAlcoholDrinkBlock(VoxelShape shapeNS, VoxelShape shapeEW, VoxelShape bigShapeNS, VoxelShape bigShapeEW) {
        super(shapeNS, shapeEW, bigShapeNS, bigShapeEW);
        this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(DOUBLE_INGREDIENTS, false)
                .setValue(ALCOHOL, 0);
    }

    @Override
    public ItemStack getCloneItemStack(BlockGetter pLevel, BlockPos pPos, BlockState pState) {
        var stack = super.getCloneItemStack(pLevel, pPos, pState);
        int alcohol = pState.getValue(ALCOHOL);
        if (alcohol > 0) {
            stack.getOrCreateTag().putInt("Alcohol", alcohol);
        }
        return stack;
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
        pBuilder.add(FACING).add(DOUBLE_INGREDIENTS).add(ALCOHOL);
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext pContext) {
        var state = super.getStateForPlacement(pContext);
        var item = pContext.getItemInHand();
        if (item.getItem() instanceof DrinkItem && state != null) {
            if (item.getTag() != null && item.getTag().contains("Alcohol")) {
                state = state.setValue(ALCOHOL, Mth.clamp(item.getTag().getInt("Alcohol"), 0, 20));
            }
        }
        return state;
    }
}
